package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyCatalogMongoRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.vo.DestCatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogMongoRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private DestinationServiceImpl destinationService;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        Destination destination = destinationService.find(strategyCatalog.getDestId());
        strategyCatalog.setDestName(destination.getName());

        repository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        Destination destination = destinationService.find(strategyCatalog.getDestId());
        strategyCatalog.setDestName(destination.getName());
        repository.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog find(String id) {
        StrategyCatalog strategyCatalog = repository.findById(id).orElse(null);

        return strategyCatalog;
    }

    @Override
    public List<StrategyCatalog> findAll() {
        List<StrategyCatalog> list = repository.findAll();
        return list;
    }

    @Override
    public Page query(StrategyCatalogQueryObject qo) {
        Query query = new Query();
        long count = template.count(query, StrategyCatalog.class);
        if(count == 0){
            return Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "sequence");
        query.with(page);
        List<StrategyCatalog> list = template.find(query, StrategyCatalog.class);
        return new PageImpl(list,page,count);
    }

    @Override
    public List<DestCatalogVO> queryCatalogByDest() {
        List<DestCatalogVO> destCatalogVOs = new ArrayList<>();
        //查询攻略分类中所有的目的地
        List<StrategyCatalog> list = this.findAll();
        Set<String> set = new TreeSet<>();
        for (StrategyCatalog strategyCatalog : list) {
            set.add(strategyCatalog.getDestName());
        }

        for (String destName : set) {
            DestCatalogVO destCatalogVO = new DestCatalogVO();
            List<StrategyCatalog> catalogs = repository.findByDestName(destName);
            destCatalogVO.setDestName(destName);
            destCatalogVO.setMapList(catalogs);
            destCatalogVOs.add(destCatalogVO);
        }
        /*  for (StrategyCatalog catalog : list) {
            //根据catalog中的destName查询出对应的攻略分类
            List<StrategyCatalog> catalogs = repository.findByDestName(catalog.getDestName());
            //将名字和攻略分类列表添加到vo集合中
            new DestCatalogVO().setDestName();
        }
*/
        return destCatalogVOs;
    }

    @Override
    public List<DestCatalogVO> groupList() {
        List<DestCatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults(); //将结果转化成list<map>   map描述一行数据
        for (Map data : datas) {
            DestCatalogVO vo = new DestCatalogVO();

            vo.setDestName(data.get("_id").toString());  //id
            List<Object> names = (List<Object>) data.get("names");  //攻略分类的名称集合
            List<Object> ids = (List<Object>) data.get("ids");  //攻略分类的ids集合

            List<StrategyCatalog> catalogList = new ArrayList<>(); //攻略分类对象

            for(int i = 0;i < names.size(); i++){
                StrategyCatalog sc = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);
            }
            vo.setMapList(catalogList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> queryStrategyDetail(String destId) {
        //根据目的地Id查询攻略分类列表
        List<StrategyCatalog> strategyCatalogs = repository.findByDestId(destId);
        System.out.println(strategyCatalogs);
        //根据攻略分类Id查询攻略明细
        for (StrategyCatalog strategyCatalog : strategyCatalogs) {
            List<Strategy> strategies = strategyService.findByCatalogId(strategyCatalog.getId());
            strategyCatalog.setStrategies(strategies);
        }

        return strategyCatalogs;
    }


}
